package com.fz.song.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.fz.song.pojo.BlogType;
import com.fz.song.service.IBlogTypeService;
import com.fz.song.util.DateUtil;
import com.fz.song.util.ResultObj;
import com.fz.song.util.UUIDUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("blogtype")
public class BlogTypeController {


    @Autowired
    private IBlogTypeService blogTypeService;

    @RequestMapping("blog_type_list_vlew.html")
    public String blog_type_list_vlew_html() {
        return "admin/blogtype/blog_type_list";
    }

    @RequestMapping("blog_type_list_data")
    public String blog_type_list_data(HttpServletRequest request, Integer page, @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        //获取数据
        PageInfo<BlogType> pageInfo = blogTypeService.getBlogTypeListData(page, limit);
        request.setAttribute("pageInfo", pageInfo);
        return "admin/blogtype/blog_type_list_data";
    }

    @RequestMapping("go_blogtype_saveorupdate.html")
    public String go_blogtype_saveorupdate_html(HttpServletRequest request, String id) {
        if (null != id && !"".equals(id)) {
            BlogType blogType = blogTypeService.getBlogTypeByid(id);
            request.setAttribute("blogType", blogType);
        }

        return "admin/blogtype/blog_type_save_or_update";
    }

    @RequestMapping("save_blogtype")
    @ResponseBody
    public Object save_blogtype(HttpServletRequest request, String jsonstr) {
        BlogType blogType = JSONObject.parseObject(jsonstr, BlogType.class);
        if (blogType.getId() == null || "".equals(blogType.getId())) {
            blogType.setId(UUIDUtil.getUUID());
            blogType.setCreatetime(DateUtil.getDateNow());
            blogType.setUpdatetime(DateUtil.getDateNow());
            blogType.setEffective(0);
            blogType.setDel(0);
            return blogTypeService.saveBlogType(blogType);
        } else {
            BlogType blogType_db = blogTypeService.getBlogTypeByid(blogType.getId());
            blogType_db.setUpdatetime(DateUtil.getDateNow());
            blogType_db.setTypeName(blogType.getTypeName());
            return blogTypeService.updateBlogType(blogType);
        }
    }


    @RequestMapping("updateStatus")
    @ResponseBody
    public Object updateStatus(HttpServletRequest request, String id) {
        BlogType blogType_db = blogTypeService.getBlogTypeByid(id);
        Integer effective = blogType_db.getEffective();
        if (effective == 1) {
            blogType_db.setEffective(0);
        } else {
            blogType_db.setEffective(1);
        }
        ResultObj resultObj = blogTypeService.updateBlogType(blogType_db);
        return resultObj;
    }
    @RequestMapping("delBlogType")
    @ResponseBody
    public Object delBlogType(HttpServletRequest request, String id) {
        ResultObj resultObj = blogTypeService.delBlogType(id);
        return resultObj;
    }
}
